import express from 'express'; // 数据库操作
import { statusList, sequelizeStatus } from '../../common/status'; // 状态码对照
import { User, Delivery, Item, Message, Order, Sku, TradeForeign } from '../../modal/index'; // 数据库实体类

const router = express.Router();

router.post('/', function (Request, Response, next) {
  Message.Modal.sync()
  let param = Request.body;
  User.Modal.findAll({
    where: {
      username: param.username,
      password: param.password
    }
  }).then(res => {
    console.log(res)
    if (res && res.length > 0) {
      // 构造token并存储
      let tokenOrigin = `{"uid": "${res[0].id}", "timeStamp": "${new Date().valueOf()}"}`
      Response.cookie("token", tokenOrigin, { maxAge: 1000 * 60 * 60 * 24, httpOnly: true, signed: true });
      Response.json({
        data: "",
        message: "success"
      });
    } else {
      Response.status(300).send({ message: "账户或密码错误" });
    }
  }).catch(err => {
    console.log(err.name)
    Response.status(500).send({ message: sequelizeStatus[err.name] || statusList[500] || err.name });
  });


});



module.exports = router;